package com.ttg.web.service.Impl;

import com.ttg.common.enums.GradeTypeEnum;
import com.ttg.common.enums.RulesTypeEnum;
import com.ttg.model.dto.SaveGrowthAndIntegralDto;
import com.ttg.model.pojo.*;
import com.ttg.web.dao.*;
import com.ttg.web.service.VipGrowthIntegrationManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangHongYuan
 * @description
 * @date 2023/8/14 17:09
 */
@Service
public class VipGrowthIntegrationManagementServiceImpl implements VipGrowthIntegrationManagementService {
    @Autowired
    private VipGrowDetailDao vipGrowDetailDao;

    @Autowired
    private VipInfoDao couponVipDao;

    @Autowired
    private BankPublicPrimitiveDao bankPublicPrimitiveDao;

    @Autowired
    private VipGradeConfigDao vipGradeConfigDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  boolean saveGrowthAndIntegral(SaveGrowthAndIntegralDto dto){

        if (Objects.isNull(dto)){
            return false;
        }
        if (Objects.isNull(dto.getMctNo())&&Objects.isNull(dto.getVipId())){
            return false;
        }

        VipGrowDetail vipGrowDetail = new VipGrowDetail();
        vipGrowDetail.setTaskId(dto.getRulesId());

//        Long  nowGrow = 0L;
        Long integrationAmount =0L;
        if (Objects.nonNull(dto.getMctNo())){
            BankPublicPrimitive byMctNo = bankPublicPrimitiveDao.getByMctNo(dto.getMctNo() + "");
            if (Objects.isNull(byMctNo)){
                return false;
            }
//            nowGrow = byMctNo.getMctNowGrow();
            integrationAmount = byMctNo.getMctIntegrationAmount();
            vipGrowDetail.setMctNo(dto.getMctNo());
            dto.setMctId(byMctNo.getId());
//            vipGrowDetail.setMainName(String.valueOf(dto.getMctNo()));
            vipGrowDetail.setType(1);
        }else {
            VipInfo byId = couponVipDao.getById(dto.getVipId());
            if (Objects.isNull(byId)){
                return false;
            }
//            nowGrow = byId.getVipNowGrow();
            integrationAmount = byId.getVipIntegrationAmount();
//            vipGrowDetail.setActNo(dto.getBankAccount());
//            vipGrowDetail.setMainName(String.valueOf(dto.getBankAccount()));
            vipGrowDetail.setVipId(byId.getVipId());
            vipGrowDetail.setType(0);
        }

        RulesTypeEnum byCode = RulesTypeEnum.getByCode(dto.getRulesId().intValue());
        LocalDate localDate = LocalDate.now();
        LocalDate dateFirstDayOfLastMonth = localDate.withDayOfMonth(1);
        LocalDate dateLastDayOfLastMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
        List<VipGrowDetail> vipDetailByVipIdAndTime = vipGrowDetailDao.getVipDetailByVipIdAndTime(vipGrowDetail.getVipId(),vipGrowDetail.getMctNo(), dto.getRulesId(),dateFirstDayOfLastMonth, dateLastDayOfLastMonth);
//        long sum = vipDetailByVipIdAndTime.stream().mapToLong(VipGrowDetail::getGrowNum).sum();
//        Long growthValue =dto.getGrowthValue() + nowGrow;
//        if (sum>=dto.getMaxGrowth()){
//            growthValue = nowGrow;
//            dto.setGrowthValue(0L);
//        }else if (sum + dto.getGrowthValue()>dto.getMaxGrowth()){
//            long l = nowGrow + dto.getGrowthValue() - dto.getMaxGrowth();
//            growthValue =nowGrow + l;
//            dto.setGrowthValue(l);
//        }
        vipGrowDetail.setTaskName(byCode.getName());
//        vipGrowDetail.setGrowNum(dto.getGrowthValue());
//        vipGrowDetail.setChangeGrowNum(growthValue.intValue());
        vipGrowDetail.setUpdateTime(LocalDateTime.now());
        vipGrowDetail.setIntegral(dto.getIntegral().intValue());
        vipGrowDetailDao.save(vipGrowDetail);

        Long l = integrationAmount + dto.getIntegral();
        List<VipGradeConfig> vipGradeByType = null;
        if (vipGrowDetail.getType()==1){
             vipGradeByType = vipGradeConfigDao.getVipGradeByType(GradeTypeEnum.TYPE_PRIVATE.getValue());
            BankPublicPrimitive bankPublicPrimitive = new BankPublicPrimitive();
            bankPublicPrimitive.setId(dto.getMctId());
//            bankPublicPrimitive.setMctAgoGrow(nowGrow);
//            bankPublicPrimitive.setMctNowGrow(growthValue);
            bankPublicPrimitive.setUpdateTime(LocalDate.now());
            return bankPublicPrimitiveDao.updateById(bankPublicPrimitive);
        }

        VipInfo vipInfo = new VipInfo();
        vipInfo.setVipId(dto.getVipId());
        vipInfo.setVipIntegrationAmount(l);
//        vipInfo.setVipNowGrow(growthValue);
//        vipInfo.setVipAgoGrow(nowGrow);
        vipInfo.setUpdateTime(LocalDateTime.now());
        return couponVipDao.updateById(vipInfo);
    }
}
